package com.sg.business.commons.ui.jsoneditor;

import java.util.Map.Entry;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;

public class JsonTreeContentProvider implements ITreeContentProvider {



	@Override
	public void dispose() {
	}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
	}

	@Override
	public Object[] getElements(Object inputElement) {
		return getChildren(inputElement);
	}

	@Override
	public Object[] getChildren(Object parentElement) {
		if(parentElement instanceof BasicDBList){
			return ((BasicDBList) parentElement).toArray();
		}else if(parentElement instanceof BasicDBObject){
			return ((BasicDBObject) parentElement).entrySet().toArray();
		}else if(parentElement instanceof Entry<?, ?>){
			return getChildren(((Entry<?,?>) parentElement).getValue());
		}else{
			return new Object[0];
		}
	}

	@Override
	public Object getParent(Object element) {
		return null;
	}

	@Override
	public boolean hasChildren(Object element) {
		return getChildren(element).length>0;
	}

}
